package com.ykt.validator;

import com.jfinal.core.Controller;

/**
 * 用户API参数校验
 *
 * @author long.hua
 * @version 1.0.0
 * @since 2015-12-10 00:09
 */
public class UserValidator{


    /**登录校验器*/
    public static class LoginValidator extends BaseValidator {

        @Override
        protected void validate(Controller c) {
            validateRequiredString("phone", "phoneMsg", "手机号不能为空！");
            validateRequiredString("checkCode", "checkCodeMsg", "短信验证码不能为空！");
        }

        @Override
        protected void handleError(Controller c) {
            c.keepPara("phone");
            c.keepPara("checkCode");

            c.renderJson(super.toJsonText(c));
        }
    }

    /**注册校验器*/
    public static class RegisterValidator extends BaseValidator {

        @Override
        protected void validate(Controller c) {
            validateRequiredString("phone", "phoneMsg", "手机号不能为空！");
            validateRequiredString("checkCode", "checkCodeMsg", "短信验证码不能为空！");
        }

        @Override
        protected void handleError(Controller c) {
            c.keepPara("phone");
            c.keepPara("checkCode");

            c.renderJson(super.toJsonText(c));
        }
    }

    /**更新用户信息校验器*/
    public static class UpdateValidator extends BaseValidator {

        @Override
        protected void validate(Controller c) {
            String name = c.getPara("name");
            String email = c.getPara("email");
            if((name == null || name.isEmpty()) && (email == null || email.isEmpty())){
                addError("nameEmailMsg", "name和email不能同时为空！");
            }

            if(email != null && !email.isEmpty()){
                String regex = "^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$";
                validateRegex("email", regex, "emailMsg", "邮箱格式不正确！");
            }
        }

        @Override
        protected void handleError(Controller c) {
            c.renderJson(super.toJsonText(c));
        }
    }


    /**更新头像校验器*/
    public static class UpdateHeadValidator extends BaseValidator {

        @Override
        protected void validate(Controller c) {
            validateRequiredString("userId", "userIdMsg", "用户ID不能为空！");
            validateRequiredString("headFile", "headMsg", "用户头像不能为空！");
        }

        @Override
        protected void handleError(Controller c) {
            c.renderJson(super.toJsonText(c));
        }
    }

    /**更新头像校验器*/
    public static class FeedbackValidator extends BaseValidator {

        @Override
        protected void validate(Controller c) {
            validateRequired("userId", "userIdMsg", "用户ID不能为空！");
            validateRequiredString("feedback", "feedbackMsg", "反馈内容不能为空！");

            String email = c.getPara("email");
            if(email != null && !email.isEmpty()){
                String regex = "^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$";
                validateRegex("email", regex, "emailMsg", "邮箱格式不正确！");
            }
        }

        @Override
        protected void handleError(Controller c) {
            c.renderJson(super.toJsonText(c));
        }
    }
}

